﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
namespace BBMS.DLL.Forms
{
    public partial class frmImportUpdate : Base.FrmEditBase
    {
        public frmImportUpdate(BBMS.DLL.Base.ObjectBase obj)
        {
            InitializeComponent();
            curObj = obj;
        }
        public new static readonly string FunctionKey = "CT_HDN";
        protected override string GetFunctionKey() { return "CT_HDN"; }
        private bool updateNgayTTDuKien = false;
        protected override void OnLoad()
        {
            if (ObjectLists.QuayList.GetKeys() != null)
            {
                foreach (object key in ObjectLists.QuayList.GetKeys())
                {
                    cbxMaQuay.Items.Add(key);
                }
            }
            if (ObjectLists.NhaCungCapList.GetKeys() != null)
            {
                foreach (object key in ObjectLists.NhaCungCapList.GetKeys())
                {
                    cbxMaNCC.Items.Add(key);
                }
            }

            if (curObj != null)
            {
                ((HoaDonNhap)curObj).DiscountList.LoadAccountedDiscount();
                txtMa.Text = ((HoaDonNhap)curObj).MaCT;
                txtSo.Text = ((HoaDonNhap)curObj).SoCT;
                txtMa.ReadOnly = true;
                txtSo.ReadOnly = true;

                dtNgayCT.Value = ((HoaDonNhap)curObj).NgayCT;
                dtNgayHT.Value = ((HoaDonNhap)curObj).NgayHT;
                dteNgayTTDukien.Value = ((HoaDonNhap)curObj).NgayTTDuKien;

                for (int i = 0; i < cbxMaNCC.Items.Count; i++) {
                    if (cbxMaNCC.Items[i].ToString().Equals(((HoaDonNhap)curObj).MaNCC))
                    {
                        cbxMaNCC.SelectedIndex = i;
                        break; 
                    }
                }

                for (int i = 0; i < cbxMaQuay.Items.Count; i++)
                {
                    if (cbxMaQuay.Items[i].ToString().Equals(((HoaDonNhap)curObj).MaQuay))
                    {
                        cbxMaQuay.SelectedIndex = i;
                        break;
                    }
                }

                txtMaHoSo.Text = ((HoaDonNhap)curObj).MaHoSo;

                txtGiaTriHangHoa.Value = ((HoaDonNhap)curObj).GiaTriHangHoa;
                txtChietKhau.Value = ((HoaDonNhap)curObj).ChietKhau;
                txtGiaTriChuaThue.Value = ((HoaDonNhap)curObj).GiaTriChuaThue;
                txtThueSuat.Value = ((HoaDonNhap)curObj).ThueSuat;
                txtGiaTriThue.Value = ((HoaDonNhap)curObj).GiaTriThue;
                txtGiaTriCT.Value = ((HoaDonNhap)curObj).GiaTriCT;
                txtGhiChu.Text = ((HoaDonNhap)curObj).GhiChu;
                dtNgayCT.Focus();

                if(!Config.CurUser.HaveRole(Role.Edit,GetFunctionKey())){
                    txtMa.ReadOnly = true;
                    txtSo.ReadOnly = true;

                    dtNgayCT.Enabled = false;
                    dtNgayHT.Enabled = false;
                    dteNgayTTDukien.Enabled = false;

                    cbxMaNCC.Enabled = false;
                    cbxMaQuay.Enabled = false;

                    txtMaHoSo.ReadOnly = true;

                    txtGiaTriHangHoa.ReadOnly = true;
                    txtChietKhau.ReadOnly = true;
                    txtGiaTriChuaThue.ReadOnly = true;
                    txtThueSuat.ReadOnly = true;
                    txtGiaTriThue.ReadOnly = true;
                    txtGiaTriCT.ReadOnly = true;
                    txtGhiChu.ReadOnly = true;
                }
            }
            else
            {
                txtMa.Focus();
            }
            updateNgayTTDuKien = true;
        }

        protected override ObjectBase GetEditedObject() {
            if (txtMa.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập ký hiệu hóa đơn.");
                txtMa.Focus();
                return null;
            }
            if (txtSo.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập số hóa đơn.");
                txtSo.Focus();
                return null;
            }
            NhaCungCap ncc = (NhaCungCap)ObjectLists.NhaCungCapList.Get(cbxMaNCC.Text.Trim());
            if (ncc == null)
            {
                MessageBox.Show("Hãy nhập thông tin nhà cung cấp.");
                cbxMaNCC.Focus();
                return null;
            }

            Quay quay = (Quay)ObjectLists.QuayList.Get(cbxMaQuay.Text.Trim());
            if (quay == null)
            {
                MessageBox.Show("Hãy nhập thông tin quầy.");
                cbxMaQuay.Focus();
                return null;
            }
            if (txtGiaTriChuaThue.Value == 0)
            {
                MessageBox.Show("Giá trị hóa đơn không hợp lệ.");
                txtGiaTriChuaThue.Focus();
                return null;
            }
            if (txtThueSuat.Value * txtGiaTriThue.Value == 0 && txtGiaTriThue.Value + txtThueSuat.Value > 0)
            {
                MessageBox.Show("Tỷ lệ thuế suất và giá trị thuế không hợp lệ.");
                txtThueSuat.Focus();
                return null;
            }
            

            long id = 0;
            if(curObj != null) id = ((HoaDonNhap)curObj).ID;
            HoaDonNhap hdn = new HoaDonNhap(id,
                txtMa.Text.Trim(),
                txtSo.Text.Trim(),
                dtNgayCT.Value.Date,
                dtNgayHT.Value.Date,
                dteNgayTTDukien.Value.Date,
                dteNgayTTDukien.Value.Date,
                TrangThaiThanhToan.ChuaTT,
                txtMaHoSo.Text.Trim(),
                ncc.Ma,
                quay.Ma,
                txtGiaTriHangHoa.Value,
                txtChietKhau.Value,
                txtGiaTriChuaThue.Value,
                (int)txtThueSuat.Value,
                txtGiaTriThue.Value,
                txtGiaTriCT.Value,
                0,
                txtGhiChu.Text.Trim(),
                Config.CurUser.UserID);
            if(discountList != null)
                hdn.DiscountList = discountList;
            return hdn;
        }

        protected override void Reset()
        {
            discountList = null;
            txtMa.ReadOnly = false;
            txtSo.ReadOnly = false;
            txtMa.Text = "";
            txtSo.Text = "";

            dtNgayCT.Enabled = true;
            dtNgayHT.Enabled = true;
            dteNgayTTDukien.Enabled = true;

            dtNgayCT.Value = DateTime.Today.Date;
            dtNgayHT.Value = DateTime.Today.Date;
            dteNgayTTDukien.Value = DateTime.Today.Date;

            cbxMaNCC.Enabled = true;
            cbxMaQuay.Enabled = true;
            cbxMaQuay.SelectedIndex = -1;
            cbxMaNCC.SelectedIndex = -1;

            txtMaHoSo.ReadOnly = false;
            txtMaHoSo.Text = "";

            txtGiaTriHangHoa.ReadOnly = false;
            txtChietKhau.ReadOnly = false;
            txtGiaTriChuaThue.ReadOnly = false;
            txtThueSuat.ReadOnly = false;
            txtGiaTriThue.ReadOnly = false;

            txtGiaTriHangHoa.Value = 0;
            txtChietKhau.Value = 0;
            txtGiaTriChuaThue.Value = 0;
            txtThueSuat.Value = 0;
            txtGiaTriThue.Value = 0;
            txtGiaTriCT.Value = 0;

            txtGhiChu.ReadOnly = false;
            txtGhiChu.Text = "";

            if (curObj != null) OnLoad();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnClose();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        
        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            OnSaveAndNew();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            OnSaveAndClose();
        }

        private void cbxMaNCC_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = cbxMaNCC.Text.Trim();
            NhaCungCap obj = (NhaCungCap)ObjectLists.NhaCungCapList.Get(str);
            if (obj != null)
            {
                txtTenNCC.Text = obj.Ten;
                txtMST.Text = obj.MSTVAT;
                txtDC.Text = obj.DiaChi;
            }
            else
            {
                txtTenNCC.Text = "";
                txtMST.Text = "";
                txtDC.Text = "";
            }
            UpdateNgayTTDuKien();
        }

        private void cbxMaQuay_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = cbxMaQuay.Text.Trim();
            Quay obj = (Quay)ObjectLists.QuayList.Get(str);
            if (obj != null)
            {
                txtTenQuay.Text = obj.Ten;
            }
            else
            {
                txtTenQuay.Text = "";
            }
        }

        private void dtNgayCT_ValueChanged(object sender, EventArgs e)
        {
            if (dtNgayCT.Value > dtNgayHT.Value)
                dtNgayHT.Value = dtNgayCT.Value;
        }

        private void dtNgayHT_ValueChanged(object sender, EventArgs e)
        {
            if (dtNgayCT.Value > dtNgayHT.Value)
                dtNgayHT.Value = dtNgayCT.Value;
            UpdateNgayTTDuKien();
        }
        private void UpdateNgayTTDuKien()
        {
            if (updateNgayTTDuKien)
            {
                string str = cbxMaNCC.Text.Trim();
                dteNgayTTDukien.Value = BillHelper.TinhNgayThanhToanDuKien(str, dtNgayHT.Value);
            }
        }

        private void txtGiaTriGoc_TextChanged(object sender, EventArgs e)
        {
            if(updateNgayTTDuKien)
                txtGiaTriChuaThue.Value = txtGiaTriHangHoa.Value - txtChietKhau.Value;
        }

        private void txtChietKhau_TextChanged(object sender, EventArgs e)
        {
            if(updateNgayTTDuKien)
                txtGiaTriChuaThue.Value = txtGiaTriHangHoa.Value - txtChietKhau.Value;
        }

        private void txtGiaTri_TextChanged(object sender, EventArgs e)
        {
            if (updateNgayTTDuKien)
            {
                txtGiaTriThue.Value = (txtGiaTriChuaThue.Value * txtThueSuat.Value) / 100;
                txtGiaTriCT.Value = txtGiaTriChuaThue.Value + txtGiaTriThue.Value;
            }
        }

        private void txtThueSuat_TextChanged(object sender, EventArgs e)
        {
            if(updateNgayTTDuKien)
                txtGiaTriThue.Value = (txtGiaTriChuaThue.Value * txtThueSuat.Value) / 100;
        }

        private void txtGiaTriThue_TextChanged(object sender, EventArgs e)
        {
            if(updateNgayTTDuKien)
                txtGiaTriCT.Value = txtGiaTriChuaThue.Value + txtGiaTriThue.Value;
        }
        private DiscountList discountList = null;
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            string str = cbxMaNCC.Text.Trim();
            NhaCungCap ncc = (NhaCungCap)ObjectLists.NhaCungCapList.Get(str);
            if (ncc == null)
            {
                MessageBox.Show("Hãy chọn nhà cung cấp");
                return;
            }
            long id = 0;
            if (curObj != null) id = ((HoaDonNhap)curObj).ID;
            
            frmImportDiscount2 frm;
            discountList = new DiscountList(id);
            if (curObj != null) discountList = ((HoaDonNhap)curObj).DiscountList;
            frm = new frmImportDiscount2(ncc, id, txtChietKhau.Value, discountList);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtChietKhau.Value = frm.ChietKhau;
                discountList = frm.DiscountList;
            }
        }
    }
}
